<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-form :inline="true" :model="listQuery" size="mini">
        <el-row>
          <el-col :span="24">
            <el-form-item label="">
              <el-input
                v-model="listQuery.order_no"
                placeholder="单据号/姓名"
                clearable
                @keyup.enter.native="handleFilter"
              >
              </el-input>
            </el-form-item>
              <el-form-item label="">
                <el-select
                  v-model="listQuery.account"
                  placeholder="银行账号"
                  clearable
                >
                  <el-option
                    v-for="item in accounts"
                    :label="item.name"
                    :value="item.id"
                    :key="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="">
                <el-select
                  v-model="listQuery.customer"
                  placeholder="相关客商名称"
                  clearable
                >
                  <el-option
                    v-for="item in customers"
                    :label="item.name"
                    :value="item.id"
                    :key="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            <el-form-item label="">
              <el-date-picker
                v-model="listQuery.search_date"
                type="daterange"
                align="right"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>

            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                size="mini"
                @click.native="handleFilter"
                >查询</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
        <el-row> </el-row>
      </el-form>
      <el-table
        :data="journals"
        border
        v-loading="listLoading"
        fit
        size="mini"
        highlight-current-row
        show-summary
        :summary-method="getSummaries"
      >
        <el-table-column
          type="index"
          label="#"
          align="center"
        ></el-table-column>
        <el-table-column
          v-for="item in tableColumns"
          :label="item.title"
          :prop="item.key"
          :key="item.key"
          :width="item.width"
          :align="item.align ? item.align : 'center'"
          :formatter="dataFormatter"
        ></el-table-column>
      </el-table>
      <!-- 分页 -->
      <!-- <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getJournals"
      /> -->
    </el-card>
  </div>
</template>

<script>
import moment from "moment";
import { mapGetters } from "vuex";
import {pickerOptions } from "@/utils/const"
import { getAccountList, getJournals } from "@/api/finance";
import { getCustomSupplierList } from "@/api/base";

import Pagination from "@/components/Pagination";

export default {
  name: "Journal",

  data() {
    return {
      title: "流水账",
      journals: [],
      accounts: [],
      customers: [],
      total: 0,
      listLoading: true,
      listQuery: {
        search_date: [
          moment().add(-30, "days").format("YYYY-MM-DD"),
          moment().format("YYYY-MM-DD"),
        ],
        account: null,
        customer: null,
        order_no: null,
      },
      tableColumns: [
        {
          key: "customer_name",
          title: "客户名称",
        },
        {
          key: "account_name",
          title: "银行名称",
        },
        {
          key: "order_no",
          title: "单据号",
          width: "140",
        },
        {
          key: "order_time",
          title: "单据日期",
        },
        
        {
          key: "money",
          title: "发生金额",
        },
        {
          key: "account_balance",
          title: "余额",
        },
        {
          key: "source",
          title: "单据来源",
        },
        {
          key: "desc",
          title: "备注",
        },
        {
          key: "user_name",
          title: "经手人",
        },
      ],
      pickerOptions,
    };
  },
  computed: {
    ...mapGetters(["uid"]),
  },
  components: {
    Pagination,
  },
  created() {
    this.getJournals();
    this.getAccountList();
    this.getCustomers();
  },
  methods: {
    // 流水账列表
    getJournals() {
      this.listLoading = true;
      getJournals(this.listQuery)
        .then((res) => {
          this.journals = res;
          this.listLoading = false;
        })
        .catch((error) => {
          console.log(error.message);
          this.$message.error("获取数据失败");
          this.listLoading = false;
        });
    },

    getCustomers() {
      getCustomSupplierList().then((res) => {
        this.customers = res;
      });
    },

    getAccountList() {
      getAccountList().then((res) => {
        this.accounts = res;
      });
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 1) {
          sums[index] = "合计";
          return;
        }
        if (column.property === "money") {
          const values = data.map((item) => item[column.property]);
          sums[index] = values
            .reduce((prev, curr) => {
              return prev + curr;
            }, 0)
            .toFixed(2);
          sums[index] += " 元";
        } else {
          return;
        }
      });
      return sums;
    },
    dataFormatter(row, column, cellValue, index) {
      if (column.property == "money" || column.property == "account_balance") {
        return cellValue && cellValue.toFixed(2);
      } else {
        return cellValue;
      }
    },
    // 查询
    handleFilter() {
      this.getJournals();
    },
  },
};
</script>

<style lang='scss' scoped>
.app-container {
  // .box-card{
  //   margin-top: 30px;
  // }
  .el-table {
    margin-top: 10px;
    width: 100%;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
  .el-select {
    width: 100%;
  }
  .item {
    margin-top: 8px;
  }
}
</style>